﻿using DataRepositoryLayer.Classes;
using DataRepositoryLayer.Classes;
using DataRepositoryLayer.Interfaces;
using ModelLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Data;

namespace BusinessLogicLayer
{
   public class StationManager
    {

        public IList<Station> GetAllStations()
        {
            IList<Station> stations = null;
            using (IUnitOfWork context = new UnitOfWork())
            {
                IStationRepository stationRep = new StationRepository(context);
                stations = stationRep.GetAll(s => s.Pricings, b => b.Batteries);
                return stations;
            }
        }

        public Station GetStationById(int id)
        {
            using (IUnitOfWork context = new UnitOfWork())
            {
                IStationRepository stationRep = new StationRepository(context);
                return stationRep.GetByIdWithBatteries(id);  //Write clean code, no need to make a seperate variable here, just straight return it
                //don't worry the using will still work even if you do that
            }
        }
       public int AddNewStation(Station station)
        {
            using (IUnitOfWork context = new UnitOfWork())
            {
                context.BeginTransaction(IsolationLevel.Serializable);
                IStationRepository stationRep = new StationRepository(context);
                stationRep.Add(station);
                context.Commit();
                IBatteryTypeRepository batTypeRep = new BatteryTypeRepository(context);
                IList<BatteryType> allTypes = batTypeRep.GetAll();
                IPricingRepository pricingRep = new PricingRepository(context);
                IList<Pricing> pricingsToAdd = new List<Pricing>();
                foreach(BatteryType type in allTypes)
                {
                    pricingsToAdd.Add(new Pricing() { battery_type_id = type.id, station_id = station.id, price = 0 });
                }
                pricingRep.AddMultiple(pricingsToAdd.ToArray());

                context.Commit();
                context.CommitTransaction();
                return station.id;
                
            }
        }

       public void UpdateStation(Station station)
       {
           using(IUnitOfWork context = new UnitOfWork())
           {
               IStationRepository stationRep = new StationRepository(context);
               stationRep.Update(station);
               context.Commit();
           }
       }

       public void RemoveStation(Station station)
       {
           using(IUnitOfWork context = new UnitOfWork())
           {
               IStationRepository stationRep = new StationRepository(context);
               stationRep.Remove(station);
               context.Commit(); //yes exactly this, no need for transaction here the EF is always making one by itself when you change sth :)
           }

          
       }

       public int Test(int number)
       {
           return number;
       }
     

    }
}
